﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreFrame.Business;
using MarchEstate.Manager.Dao.Init;
using System.Collections;
using System.Data;
using MarchEstate.Manager.BusinesEntity;
using CoreFrame.Core.Aop;
using CoreFrame.Core.Transaction;

namespace MarchEstate.Manager.ObjectModel.Init
{
    public class SysInitDataManager : AbstractGenericModel
    {
        private SysInitDao sysDao = null;

        public SysInitDataManager()
        {

        }
        public override void InitDao()
        {
            base.InitDao();
            sysDao = NewDao<SysInitDao>();
        }

        public override object OperationDataModel(string method, object value)
        {
            switch (method)
            {
                case "BatchBindingItem":
                    BatchBindingItem(value);
                    break;
                case "BatchBingingMeter":
                    BatchBingingMeter(value);
                    break;
            }
            return base.OperationDataModel(method, value);
        }

        public override object OperationDataModel(string method, object value, CoreFrame.Core.Orm.PageInfo page)
        {
            switch (method)
            {
                case "GetRoomList":
                    return sysDao.GetRoomList(((Hashtable)value)["areacode"].ToString(), ((Hashtable)value)["buildcode"].ToString(), ((Hashtable)value)["roomname"].ToString(), page);
                case "GetShopList":
                    return sysDao.GetShopList(value.ToString(), page);
            }

            return base.OperationDataModel(method, value, page);
        }
        [AOP(typeof(AopTransaction))]
        private void BatchBindingItem(object value)
        {
            string type = ((Hashtable)value)["type"].ToString().Trim();
            string binditemId = ((Hashtable)value)["binditemId"].ToString().Trim();
            string ownerId = ((Hashtable)value)["ownerId"].ToString().Trim();
            string delitem = ((Hashtable)value)["delitem"].ToString().Trim();
            string itemcode = ((Hashtable)value)["itemcode"].ToString().Trim();
            AbstractOSRCCItemRelation srcc = NewObject<AbstractOSRCCItemRelation>();
            AbstractServiceItem item = NewObject<AbstractServiceItem>();
            string[] itemcodes = itemcode.Split(new char[] { ',' });

            if (type == "0")//批量生成住宅收费项目
            {

                DataTable dt = sysDao.GetAllRoomList();
                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    if (delitem == "1") sysDao.DeleteOSRCCItemRelation(dt.Rows[k]["RoomId"].ToString());
                    for (int i = 0; i < itemcodes.Length; i++)
                    {
                        AbstractServiceItem _item = item.GetServiceItem(itemcodes[i]);
                        AbstractOSRCCItemRelation _srcc = (AbstractOSRCCItemRelation)srcc.Clone();
                        _srcc.OwnerId = dt.Rows[k]["OwnerId"].ToString() == "" ? null : dt.Rows[k]["OwnerId"].ToString();
                        _srcc.SrccId = dt.Rows[k]["RoomId"].ToString();
                        _srcc.ItemCode = _item.Code;
                        _srcc.ItemName = _item.ItemName;
                        //如果绑定的项目已存在就不增加重复的
                        if (srcc.GetSrccItem(_srcc.SrccId, _srcc.ItemCode).SrccId==null)
                            _srcc.save();
                    }
                }

            }
            else if (type == "1")//批量生成门面收费项目
            {
                DataTable dt = sysDao.GetAllShopList();
                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    if (delitem == "1") sysDao.DeleteOSRCCItemRelation(dt.Rows[k]["ShopId"].ToString());
                    for (int i = 0; i < itemcodes.Length; i++)
                    {
                        AbstractServiceItem _item = item.GetServiceItem(itemcodes[i]);
                        AbstractOSRCCItemRelation _srcc = (AbstractOSRCCItemRelation)srcc.Clone();
                        _srcc.OwnerId = dt.Rows[k]["OwnerId"].ToString() == "" ? null : dt.Rows[k]["OwnerId"].ToString();
                        _srcc.SrccId = dt.Rows[k]["ShopId"].ToString();
                        _srcc.ItemCode = _item.Code;
                        _srcc.ItemName = _item.ItemName;
                        _srcc.save();
                    }
                }
            }
            else//添加收费项目
            {
                if (delitem == "1") sysDao.DeleteOSRCCItemRelation(binditemId);
                for (int i = 0; i < itemcodes.Length; i++)
                {
                    AbstractServiceItem _item = item.GetServiceItem(itemcodes[i]);
                    AbstractOSRCCItemRelation _srcc = (AbstractOSRCCItemRelation)srcc.Clone();
                    _srcc.OwnerId = ownerId == "" ? null : ownerId;
                    _srcc.SrccId = binditemId;
                    _srcc.ItemCode = _item.Code;
                    _srcc.ItemName = _item.ItemName;
                    _srcc.save();
                }
            }
        }
        [AOP(typeof(AopTransaction))]
        private void BatchBingingMeter(object value)
        {
            int type = Convert.ToInt32(((Hashtable)value)["type"].ToString().Trim());
            string ownerId = ((Hashtable)value)["ownerId"].ToString().Trim() == "" ? null : ((Hashtable)value)["ownerId"].ToString().Trim();
            string ownerName = ((Hashtable)value)["ownerName"].ToString().Trim();
            string srccId = ((Hashtable)value)["srccId"].ToString().Trim();
            string srccName = ((Hashtable)value)["srccName"].ToString().Trim();

            int formtype = Convert.ToInt32(((Hashtable)value)["formtype"].ToString().Trim());
            int formtype2 = Convert.ToInt32(((Hashtable)value)["formtype2"].ToString().Trim());
            int metercount = Convert.ToInt32(((Hashtable)value)["metercount"].ToString().Trim());
            string itemcode = ((Hashtable)value)["itemcode"].ToString().Trim();
            AbstractMeter meter = NewObject<AbstractMeter>();
            if (type == 0)
            {
                #region 批量绑定
                //0,公摊；1，住宅；2，门面；3，其它
                if (formtype == 0)
                {
                    sysDao.DeleteMeter(formtype, formtype2, null);
                    for (int i = 0; i < metercount; i++)
                    {
                        AbstractMeter _meter = (AbstractMeter)meter.Clone();
                        _meter.MeterType = formtype;
                        _meter.MeterType2 = formtype2;
                        _meter.OwnerId = null;
                        _meter.OwnerName = "公摊" + (i + 1).ToString();
                        _meter.SrccId = null;
                        _meter.SrccName = null;
                        _meter.ItemCode = itemcode;
                        _meter.InitialDate = DateTime.Now;
                        _meter.Save();
                    }
                }
                else if (formtype == 1)
                {
                    DataTable dt = sysDao.GetAllRoomList();
                    for (int k = 0; k < dt.Rows.Count; k++)
                    {
                        sysDao.DeleteMeter(formtype, formtype2, dt.Rows[k]["RoomId"].ToString());
                        for (int i = 0; i < metercount; i++)
                        {
                            AbstractMeter _meter = (AbstractMeter)meter.Clone();
                            _meter.MeterType = formtype;
                            _meter.MeterType2 = formtype2;
                            _meter.OwnerId = dt.Rows[k]["OwnerId"].ToString() == "" ? null : dt.Rows[k]["OwnerId"].ToString();
                            _meter.OwnerName = dt.Rows[k]["OwnerName"].ToString();
                            _meter.SrccId = dt.Rows[k]["RoomId"].ToString();
                            _meter.SrccName = dt.Rows[k]["RoomNumber"].ToString();
                            _meter.ItemCode = itemcode;
                            _meter.InitialDate = DateTime.Now;
                            _meter.Save();
                        }
                    }
                }
                else if (formtype == 2)
                {
                    DataTable dt = sysDao.GetAllShopList();
                    for (int k = 0; k < dt.Rows.Count; k++)
                    {
                        sysDao.DeleteMeter(formtype, formtype2, dt.Rows[k]["ShopId"].ToString());
                        for (int i = 0; i < metercount; i++)
                        {
                            AbstractMeter _meter = (AbstractMeter)meter.Clone();
                            _meter.MeterType = formtype;
                            _meter.MeterType2 = formtype2;
                            _meter.OwnerId = dt.Rows[k]["OwnerId"].ToString() == "" ? null : dt.Rows[k]["OwnerId"].ToString();
                            _meter.OwnerName = dt.Rows[k]["OwnerName"].ToString();
                            _meter.SrccId = dt.Rows[k]["ShopId"].ToString();
                            _meter.SrccName = dt.Rows[k]["ShopName"].ToString();
                            _meter.ItemCode = itemcode;
                            _meter.InitialDate = DateTime.Now;
                            _meter.Save();
                        }
                    }
                }
                else
                {
                    sysDao.DeleteMeter(formtype, formtype2, null);
                    for (int i = 0; i < metercount; i++)
                    {
                        AbstractMeter _meter = (AbstractMeter)meter.Clone();
                        _meter.MeterType = formtype;
                        _meter.MeterType2 = formtype2;
                        _meter.OwnerId = null;
                        _meter.OwnerName = "其他" + (i + 1).ToString();
                        _meter.SrccId = null;
                        _meter.SrccName = null;
                        _meter.ItemCode = itemcode;
                        _meter.InitialDate = DateTime.Now;
                        _meter.Save();
                    }
                }
                #endregion
            }
            else
            {
                for (int i = 0; i < metercount; i++)
                {
                    AbstractMeter _meter = (AbstractMeter)meter.Clone();
                    _meter.MeterType = formtype;
                    _meter.MeterType2 = formtype2;
                    _meter.OwnerId = ownerId;
                    _meter.OwnerName = ownerName;
                    _meter.SrccId = srccId;
                    _meter.SrccName = srccName;
                    _meter.ItemCode = itemcode;
                    _meter.InitialDate = DateTime.Now;
                    _meter.Save();
                }
            }
        }
    }
}
